﻿namespace Blaze.Editor.UI
{
    using System;
    using UnityEditor;
    using UnityEngine;
    using UnityEngine.Rendering;

    /// <summary>
    /// 用于显示Blaze特效材质面板。
    /// </summary>
    public class FXBlendShaderGUI : ShaderGUI
    {
        public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
        {
            var material = (Material) materialEditor.target;
            var singleChannel = drawBooleanProperty(material, "ENABLE_SINGLE_CHANNEL", "启用单通道？");
            var enableTint = drawBooleanProperty(material, "ENABLE_TINT", "启用变色？");
            var enableAlphaSplitting = drawBooleanProperty(material, "ENABLE_ALPHA_SPLITTING", "启用独立的Alpha通道？");
            drawBlendMode(material);

            GUILayout.Space(8);

            materialEditor.SetDefaultGUIWidths();
            for (var index = 0; index < properties.Length; ++index)
            {
                var property = properties[index];
                if ((property.flags & (MaterialProperty.PropFlags.HideInInspector | MaterialProperty.PropFlags.PerRendererData)) != MaterialProperty.PropFlags.None)
                    continue;

                if (property.name == "_TintColor" && !enableTint)
                    continue;

                if (property.name == "_AlphaTex" && !enableAlphaSplitting)
                {
                    property.textureValue = null;
                    continue;
                }

                var rect = EditorGUILayout.GetControlRect(true, materialEditor.GetPropertyHeight(property, property.displayName), EditorStyles.layerMaskField);
                materialEditor.ShaderProperty(rect, property, property.displayName);
            }
        }

        private static bool drawBooleanProperty(Material material, string keyword, string displayName)
        {
            var isEnabled = Array.IndexOf(material.shaderKeywords, keyword) >= 0;
            EditorGUI.BeginChangeCheck();
            isEnabled = EditorGUILayout.Toggle(displayName, isEnabled);
            if (EditorGUI.EndChangeCheck())
            {
                if (isEnabled)
                    material.EnableKeyword(keyword);
                else
                    material.DisableKeyword(keyword);
            }
            return isEnabled;
        }

        private CustomBlendMode drawBlendMode(Material material)
        {
            if (mBlendModeDrawer == null)
            {
                var srcBlendMode = (BlendMode) (int) material.GetFloat("_SrcBlend");
                var destBlendMode = (BlendMode) (int) material.GetFloat("_DstBlend");
                var blendOp = (BlendOp) (int) material.GetFloat("_BlendOp");
                mBlendModeDrawer = new CustomBlendModeDrawer("混合模式");
                mBlendModeDrawer.Select(blendOp, srcBlendMode, destBlendMode);
            }

            EditorGUI.BeginChangeCheck();
            var result = mBlendModeDrawer.Draw();
            if (EditorGUI.EndChangeCheck())
            {
                material.SetFloat("_BlendOp", (int) result.Op);
                material.SetFloat("_SrcBlend", (int) result.Src);
                material.SetFloat("_DstBlend", (int) result.Dest);
            }
            return result;
        }

        private CustomBlendModeDrawer mBlendModeDrawer;
    }
}